package com.me.mygdxgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class GameWorldRender {
public static final float PIXELS_PER_METER = 60f;	
private TiledMapHelper tiledMapHelper;
SpriteBatch spriteBatch;

GameWorld game;

private int screenWidth;
private int screenHeight;
	
public int getScreenWidth() {
	return screenWidth;
}






public void setScreenWidth(int screenWidth) {
	this.screenWidth = screenWidth;
}






public int getScreenHeight() {
	return screenHeight;
}






public void setScreenHeight(int screenHeight) {
	this.screenHeight = screenHeight;
}






public GameWorldRender( GameWorld game){
	
	this.game=game;
	
	screenWidth =667;
	screenHeight = 400;

	if(Gdx.graphics.getHeight()<=320 && Gdx.graphics.getWidth()<=480){
		screenWidth = Gdx.graphics.getWidth();
		screenHeight = Gdx.graphics.getHeight();
	}


	spriteBatch=new SpriteBatch();
	
	
tiledMapHelper = new TiledMapHelper(this);

tiledMapHelper.setPackerDirectory("data/packer");

tiledMapHelper.loadMap("data/world/level1/level.tmx");

tiledMapHelper.prepareCamera(screenWidth, screenHeight);

tiledMapHelper.loadCollisions("data/collisions.txt", game.world,
		PIXELS_PER_METER);
	
	
}






public void Render(){
	
	tiledMapHelper.getCamera().position.x = PIXELS_PER_METER
			* game.mario.jumper.getPosition().x;

	/**
	 * Ensure that the camera is only showing the map, nothing outside.
	 */
	if (tiledMapHelper.getCamera().position.x <screenWidth / 2) {
		tiledMapHelper.getCamera().position.x =screenWidth / 2;
	}
	if (tiledMapHelper.getCamera().position.x >= tiledMapHelper.getWidth()
			-screenWidth / 2) {
		tiledMapHelper.getCamera().position.x = tiledMapHelper.getWidth()
				-screenWidth / 2;
	}

	if (tiledMapHelper.getCamera().position.y < screenHeight / 2) {
		tiledMapHelper.getCamera().position.y = screenHeight / 2;
	}
	if (tiledMapHelper.getCamera().position.y >= tiledMapHelper.getHeight()
			- screenHeight / 2) {
		tiledMapHelper.getCamera().position.y = tiledMapHelper.getHeight()
				- screenHeight / 2;
	}

	tiledMapHelper.getCamera().update();

	tiledMapHelper.render();

	/**
	 * Prepare the SpriteBatch for drawing.
	 */
	
	TextureRegion keyFrame = null;
	
	spriteBatch.setProjectionMatrix(tiledMapHelper.getCamera().combined);
	spriteBatch.begin();
	
	
	if(game.mario.State_Pernas==game.mario.State_Stop){
		keyFrame = Assents.Mario_Pernas.getKeyFrame(game.mario.StateTime, Animation.ANIMATION_LOOPING);

		if(game.mario.direction==game.mario.State_Move_RIGHT)
		spriteBatch.draw(keyFrame, PIXELS_PER_METER * game.mario.jumper.getPosition().x
					- 64 / 2, PIXELS_PER_METER * game.mario.jumper.getPosition().y - 64 / 2,64,64 );
		else{
			spriteBatch.draw(keyFrame, PIXELS_PER_METER * game.mario.jumper.getPosition().x
					- 64 / 2 +64, PIXELS_PER_METER * game.mario.jumper.getPosition().y - 64 / 2,-1*64,64 );
		}
		
	}
		
	
	
	
	if(game.mario.State_Pernas==game.mario.State_Move_LEFT || game.mario.State_Pernas==game.mario.State_Move_RIGHT ){
	 
		keyFrame = Assents.Mario_Pernas.getKeyFrame(game.mario.StateTime, Animation.ANIMATION_LOOPING);

		if(game.mario.State_Pernas==game.mario.State_Move_RIGHT)
		spriteBatch.draw(keyFrame, PIXELS_PER_METER * game.mario.jumper.getPosition().x
					- 64 / 2, PIXELS_PER_METER * game.mario.jumper.getPosition().y - 64 / 2,64,64 );
	
		else{
			spriteBatch.draw(keyFrame, PIXELS_PER_METER * game.mario.jumper.getPosition().x
					- 64 / 2 +64, PIXELS_PER_METER * game.mario.jumper.getPosition().y - 64 / 2,-1*64,64 );
			
		}
	}


	 for(int i=0;i<game.boxes.size();i++)
		 spriteBatch.draw(Assents.b, game.boxes.get(i).x, game.boxes.get(i).y-64,32,32);
		 
	 
	 spriteBatch.draw(Assents.leftc,tiledMapHelper.getCamera().position.x-screenWidth/2+screenWidth/10,10,64,64);
	 spriteBatch.draw(Assents.rightc,tiledMapHelper.getCamera().position.x-screenWidth/2+screenWidth/10*3,10,64,64);
	 spriteBatch.draw(Assents.upc,tiledMapHelper.getCamera().position.x-screenWidth/2+screenWidth-screenWidth/10*2,10,64,64);
	 
	
	 
	 
	 
	//game.mario.jumperSprite.setPosition(
		//	PIXELS_PER_METER * game.mario.jumper.getPosition().x
			//		- game.mario.jumperSprite.getWidth() / 2,
			//PIXELS_PER_METER * game.mario.jumper.getPosition().y
				//	- game.mario.jumperSprite.getHeight() / 2);
	
	
	//game.mario.jumperSprite.draw(spriteBatch);

	/**
	 * "Flush" the sprites to screen.
	 */
	spriteBatch.end();
	
	
}	
	
	
	
	
}
